<?php

namespace app\backend\service;


use app\backend\model\ZhMatchmakerOrder;
use think\exception\ValidateException;
use think\facade\Db;
use xhadmin\CommonService;

class ZhMatchmakerOrderService extends CommonService
{

    /*
     * @Description  列表数据
     */
    public static function indexList($where, $limit, $page)
    {
        try {
            $res = ZhMatchmakerOrder::alias('o')
                ->leftjoin('zh_matchmaker ','o.m_id=zh_matchmaker.matchmaker_id')
                ->leftjoin('zh_authentication ','o.a_id=zh_authentication.authentication_id')
                ->leftjoin('zh_matchmaker_charge ','o.c_id=zh_matchmaker_charge.id')
             /*   ->leftjoin('member m','zh_matchmaker.u_id = m.uid')
                ->leftjoin('member a','zh_authentication.u_id = a.uid')*/
                  ->where($where)
                ->field("o.id,o.m_id,o.a_id,o.price,o.status,o.order_sn,o.price,o.createtime,zh_authentication.real_name,zh_matchmaker.name,
                 zh_matchmaker_charge.entry_name,zh_matchmaker_charge.comment,zh_matchmaker_charge.is_fixed_price")
                ->order(['createtime' => 'desc'])
                ->paginate(['list_rows'=>$limit,'page'=>$page])
                ->toArray();

        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return ['rows' => $res['data'], 'total' => $res['total']];
    }

    public static function byId($id)
    {
        try {
            $res = ZhMatchmakerOrder::alias('o')
                ->leftjoin('zh_matchmaker ','o.m_id=zh_matchmaker.matchmaker_id')
                ->leftjoin('zh_authentication ','o.a_id=zh_authentication.authentication_id')
                /*->leftjoin('member m','zh_matchmaker.u_id = m.uid')
                ->leftjoin('member a','zh_authentication.u_id = a.uid')*/
                ->field("o.id,o.m_id,o.a_id,o.price,o.status,o.order_sn,o.price,o.createtime,zh_authentication.real_name,zh_matchmaker.name")
                ->where(['id'=>$id])
                ->find();

        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return $res;
    }

    /*
     * @Description  修改
     */
    public static function update($data)
    {
        try {
            validate(\app\backend\validate\ZhMatchmakerOrder::class)->scene('update')->check($data);
            $data['createtime'] = strtotime($data['createtime']);
            $res                = ZhMatchmakerOrder::update($data);
        } catch (ValidateException $e) {
            throw new ValidateException ($e->getError());
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        if (!$res) {
            throw new ValidateException ('操作失败');
        }
        return $res;
    }


}